package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQuery;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiresPermissions(value = {"customer:potentialList","潜在客户列表"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo){

        //判断subject 是否含有Market_Manager,可以查询所有客户信息,若没有只能查看个人的客户信息
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))){
            //p普通登录用户
            qo.setSellerId(UserContext.getCurrentUser().getId());
        }
        //只查询潜在客户状态的数据
        qo.setStatus(Customer.STATUS_COMMON);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询职业 的下拉框数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectTitleBySn("job");
        model.addAttribute("jobs",jobs);
        //查询来源的下拉框的数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectTitleBySn("source");
        model.addAttribute("sources",sources);

        //查询带有Market,Market_Manager的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market","Market_Manager");
        model.addAttribute("sellers",sellers);

        //查询所有交流方式
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectTitleBySn("communicationMethod");
        model.addAttribute("ccts",ccts);

        /*//交流方式下拉框数据
        List<SystemDictionaryItem>*/

        return "customer/potentialList";

    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
    @RequestMapping("/poolList")
    public String changeCustomerStatus(Model model,@ModelAttribute("qo") CustomerQuery qo){

        //只查询客户池状态的数据
        qo.setStatus(Customer.STATUS_POOL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询带有Market,Market_Manager的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market","Market_Manager");
        model.addAttribute("sellers",sellers);

        return "customer/poolList";
    }

}
